package com.example.myapplication.model;


import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;

import androidx.core.app.ActivityCompat;

import com.blankj.utilcode.util.PhoneUtils;
import com.example.myapplication.BuildConfig;
import com.example.myapplication.ben.Bean;
import com.example.myapplication.retrofit.AppClient;
import com.example.myapplication.rxjava.ApiCallback;
import com.example.myapplication.rxjava.ApiStores;
import com.example.myapplication.rxjava.IBaseModel;
import com.example.myapplication.rxjava.SubscriberCallBack;
import com.example.myapplication.upData.AddAppdownload;
import com.example.myapplication.upData.LoginModel;

import java.util.HashMap;
import java.util.Map;

import rx.Observable;

/**
 * 类描述：公共接口
 */

public class CommentModel extends IBaseModel implements ICommentModel {

    private static CommentModel commentModel = null;
    public ApiStores apiStores = AppClient.retrofit().create(ApiStores.class);

    private CommentModel() {
    }

    public static CommentModel getInstant() {
        if (null == commentModel) {
            commentModel = new CommentModel();
        }
        return commentModel;
    }

    /**
     * @return 参数集合 便于加公共参数
     */
    private Map<String, String> getHashMap() {
        return new HashMap<String, String>();
    }

    /**
     * @param adType
     * @param callBack
     */
    <T> void addSubscriptions(Observable adType, final CallBack callBack) {
        addSubscription(adType, new SubscriberCallBack<>(new ApiCallback<Bean<T>>() {
            @Override
            public void onSuccess(Bean<T> bean) {
                callBack.onSuccess(bean.getResult(), true, bean.getMessage());
            }

            @Override
            public void onFailure(int code, String msg) {
                callBack.onFailure(msg);
            }

            @Override
            public void onCompleted() {

            }
        }));
    }


    @Override
    public void getHomeData(String searchName, int pager, CallBack callBack) {
//        Map<String, String> hashMap = getHashMap();
//        hashMap.put("q", searchName);
//        hashMap.put("sn", pager+"");
//        hashMap.put("pn", ApiStores.Companion.getPagerSize()+"");
//        addSubscriptions(apiStores.getHomeData( hashMap), callBack);
    }


    public void getWebViewUrl(CallBack callBack) {
        addSubscriptions(apiStores.getWebViewUrl(BuildConfig.APPLICATION_ID), callBack);
    }

    /**
     * 注册用户
     *
     * @param model
     * @param callBack
     *
     *
     */
    public void postLogin(LoginModel model, CallBack callBack) {
        addSubscriptions(apiStores.getLogin(model), callBack);
    }

    /**
     * 注册用户
     *
     * @param model
     * @param callBack
     *
     *
     */
    @SuppressLint("MissingPermission")
    public void getcatalist(CallBack callBack) {
        addSubscriptions(apiStores.getcatalist(PhoneUtils.getIMEI()), callBack);
    }
    /**
     * APP下载记录新增-添加
     *
     * @param model
     * @param callBack
     */
    public void postAddAppdownload(AddAppdownload model, CallBack callBack) {
        addSubscriptions(apiStores.postAddAppdownload(model), callBack);
    }

    public void getcersion( CallBack callBack) {
        addSubscriptions(apiStores.getcersion(BuildConfig.APPLICATION_ID,BuildConfig.VERSION_CODE+""), callBack);
    }
//    /**
//     * app评价信息-添加
//     *
//     * @param model
//     * @param callBack
//     */
//    public void postAddAppdownload(AddAppdownload model, CallBack callBack) {
//        addSubscriptions(apiStores.postAddAppdownload(model), callBack);
//    }
}
